package dog

import "sync"

type DogFactory struct {
	dogmapping map[string]DogCreater
}

type DogCreater interface {
	Create() IDog
}

type DogCreaterFunc struct {
	creater_func func() IDog
}

func (this *DogCreaterFunc) Create() IDog {
	if this.creater_func == nil {
		return nil
	}
	return this.creater_func()
}

func NewDogCreaterFunc(func_creater func() IDog) *DogCreaterFunc {
	return &DogCreaterFunc{
		creater_func: func_creater,
	}
}

var dogfactory *DogFactory = &DogFactory{
	dogmapping: make(map[string]DogCreater),
}
var mutex sync.Mutex

func GetDefaultDogFactory() *DogFactory {
	return dogfactory
}

func (this *DogFactory) RegisterCreater(name string, creater DogCreater) {
	this.dogmapping[name] = creater
}

func (this *DogFactory) CreateDog(name string) IDog {
	creater := this.dogmapping[name]
	if creater != nil {
		return creater.Create()
	} else {
		return nil
	}
}

func (this *DogFactory) Init() {
	this.RegisterCreater("Dog", NewDogCreaterFunc(func() IDog {
		return &Dog{}
	}))
}
func (this *DogFactory) GetAllDogNames() []string {
	var names []string

	for k, _ := range this.dogmapping {
		names = append(names, k)
	}

	return names
}
